package com.mmdccj.yugu.mapper;

import com.mmdccj.yugu.dto.MusicListDto;
import com.mmdccj.yugu.entity.MusicLinkList;
import com.mmdccj.yugu.entity.MusicList;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: MMDCCJ
 * @date: 2023-11-28 12:14
 */
@Mapper
public interface MusicListMapper {
    // 在歌单表中插入一条歌单
    @Insert("INSERT INTO music_list(creator_id, title, introduce, play_count, image, create_date, share_count)" +
            "VALUES(#{creatorId}, #{title}, '无', 0, 'default-image.jpg', now(), 0)")
    void insertMusicList(MusicList list);

    // 查询 8 条歌单
    @Select("SELECT * FROM music_list WHERE is_cd=FALSE LIMIT 8")
    List<MusicList> selectMusicList();

    // 查询 10 张CD
    @Select("SELECT * FROM music_list WHERE is_cd=TRUE LIMIT 10 ")
    List<MusicList> select10CD();

    // 将歌单与音乐建立连接
    @Insert("INSERT INTO music_link_list (music_id, music_list_id) VALUES (#{musicId}, #{musicListId})")
    void insertLink(String musicListId, String musicId);

    // 通过用户id获取用户所创建的歌单
    @Select("SELECT * FROM music_list WHERE creator_id = #{id} AND is_cd=FALSE")
    List<MusicList> getMusicListByUserId(String id);

    // 获取歌单/CD中的音乐数量
    @Select("SELECT COUNT(*) FROM music_link_list WHERE music_list_id = #{id}")
    int getMusicNumFromMusicListByMusicListId(String id);

    // 获取歌单的图片src
    @Select("SELECT image FROM music_list WHERE id = #{id}")
    MusicList getListImageFrom(String id);

    // 更新歌单的图片
    @Update("UPDATE music_list SET image=#{src} WHERE id=#{id}")
    void updateImage(String src, String id);

    // 歌单信息编辑处回显
    @Select("SELECT title,introduce FROM music_list WHERE id=#{id}")
    MusicList getMusicListTitleAndIntroById(String id);

    // 更新歌单的信息
    @Update("UPDATE music_list SET title=#{title},introduce=#{intro} WHERE id=#{id}")
    void updateInfo(String id, String intro, String title);

    // 获取某个用户的所有歌单
    @Select("SELECT id,title,image FROM music_list WHERE creator_id=#{id} AND is_cd=FALSE")
    List<MusicList> getUserALlList(String id);

    // 收藏歌单
    @Insert("INSERT INTO collect_music_list (collector,music_list_id) VALUES(#{userId},#{musicListId})")
    int collectMusicList(String userId, String musicListId);

    /*
     * 检查用户是否能够收藏该歌单
     * 1、已经收藏该歌单
     * 2、该歌单由此用户创建
     */
    int checkCollect(String userId, String musicListId);

    int isCreator(String userId, String musicListId);

    // 检查歌单是否存在
    @Select("SELECT COUNT(*) FROM music_list WHERE id=#{musicListId} AND is_cd=FALSE")
    int isMusicList(String musicListId);

    @Select("SELECT music_list_id FROM collect_music_list WHERE collector=#{userId} ")
    List<String> getCollectListIds(String userId);

    List<MusicList> getCollectList(List<String> collectIds);

    // 更新歌单播放量
    @Update("UPDATE music_list SET play_count=play_count+1 WHERE id=#{musicListId}")
    void updateListPlays(String musicListId);

    // 获取歌单DTO
    @Select("SELECT m.id,m.creator_id,m.title,m.image,user.name as creator_name FROM music_list m join user on user.id = m.creator_id WHERE m.is_cd=FALSE")
    List<MusicListDto> getMusicListDto();

    @Insert("INSERT INTO music_list(creator_id,title,introduce,play_count,image,create_date,share_count,is_cd) " +
            "VALUES(#{userId},#{title},#{intro},0,#{image},#{date},0,TRUE)")
    void insertCD(String userId, String title, String image, String intro, LocalDateTime date);

    @Select("SELECT id, creator_id, title, introduce, play_count, image, create_date, share_count, is_cd," +
            "(SELECT COUNT(*) FROM music_link_list WHERE music_list_id = music_list.id) as musicNum " +
            "FROM music_list WHERE creator_id=#{userId} AND is_cd=TRUE")
    List<MusicList> getUSERAllCD(String userId);

    @Select("SELECT * FROM music_list WHERE creator_id=#{userId} AND id=#{CdId} AND is_cd=TRUE")
    MusicList getCDEditInfo(String userId, String CdId);

    @Update("UPDATE music_list SET title=#{title},introduce=#{introduce},image=#{image} WHERE id=#{id} AND creator_id=#{userId} AND is_cd=TRUE")
    void updateCDInfo(String userId, String id, String title, String introduce, String image);

    void insertMusicToCD(List<MusicLinkList> link);

    @Delete("DELETE FROM music_link_list WHERE music_id=#{musicId} AND music_list_id=#{listId}")
    void removeMusicFromCD(String listId, String musicId);

    @Delete("DELETE FROM music_list WHERE id=#{id} AND creator_id=#{userId}")
    void deleteCD(String id, String userId);

    @Delete("DELETE FROM music_link_list WHERE music_list_id=#{id}")
    void deleteLink(String id);

    @Delete("DELETE FROM collect_music_list WHERE music_list_id=#{id}")
    void deleteCollect(String id);

    // 获取总歌单数量
    @Select("SELECT COUNT(*) FROM music_list")
    int selectAll();

    @Select("SELECT * FROM music_list WHERE id!=#{listId}")
    List<MusicList> getRecommendList(String listId);

    void collectMusics(String musicId, String listId);

    @Select("SELECT COUNT(*) FROM music_link_list WHERE music_list_id=#{listId} AND music_id=#{musicId}")
    int isInMusicList(String musicId, String listId);
}
